package com.voice.call.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.ConstUtil;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.service.LuckyCreditService;
import com.voice.call.service.OpLogService;
import com.voice.call.service.RedisService;
import com.voice.call.service.TaskIntegralService;
import com.voice.call.utils.IpUtil;
import com.voice.call.utils.JSONUtil;
import com.voice.call.utils.StringUtil;
import com.voice.call.version.ApiVersion;


//Lucky Credit
@RestController
@RequestMapping("/api/{version}/luckycredit/")
public class LuckyCreditController {
	
	private final Logger log = LoggerFactory.getLogger(LuckyCreditController.class);
	
	
	
	@Resource
	private TaskIntegralService taskIntegralService;
	
	@Resource
	private LuckyCreditService luckyCreditService;
	
	@Resource
	private OpLogService opLogService;
	
	@Resource
	private RedisService redisService;
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "info", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result luckycreditInfo(@RequestBody Map<String, Object> params) {
		//log.debug("参数:{}",JSONUtil.objectToJson(params));
		String lang = ConstLang.getLang(params);
		// 判断入参
		if (null == params ||  params.get("uid") == null) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		Integer user_id = Integer.valueOf(params.get("uid").toString());
		Result rs = luckyCreditService.getLuckyCreditList(user_id);
		rs.changeLange(lang);
		//log.info("luckycredit info = {}",JSONUtil.objectToJson(rs));
		return rs;
		//return taskIntegralService.getLuckyCreditList(user_id);
	}
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "play", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result addLuckycredit(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		//log.debug("参数:{}",JSONUtil.objectToJson(params));
		String lang = ConstLang.getLang(params);
		// 判断入参
		if (null == params ||  params.get("uid") == null || params.get("advert") == null) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		Integer user_id = Integer.valueOf(params.get("uid").toString());
		String advert = params.get("advert").toString();//0:没有广告，1：有广告
		
		if (StringUtil.isEmpty(advert)){
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		if (!advert.equals("0") && !advert.equals("1")) {
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		
		if (advert.equals("0")) { //表示没有广告，进行写日志
			String v = ConstUtil.getVersionName(params);
			String ip = IpUtil.getIpAddr(request);
			opLogService.luckyCreditWriteLog(user_id, v, ip);
			
			//log.info("老虎机没有广告，返回0积分");
			//没有广告生成空的积分
			Map<String, Object> rsMap = new HashMap<String, Object>();
			rsMap.put("id", 0);
			rsMap.put("integral",0);
			return new Result(rsMap,lang);
		}
		Result rs = luckyCreditService.playLuckyCredit(user_id,advert.equals("1")?true:false);
		rs.changeLange(lang);
		//log.info("luckycredit play = {}",JSONUtil.objectToJson(rs));
		return rs;
		//Integer task_id = Integer.valueOf(params.get("id").toString());
		//return taskIntegralService.playLuckyCredit(user_id, task_id);
	}
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "exchange", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result luckycreditExchange(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		
		
		
		log.debug("luckycreditExchange 参数:{}",JSONUtil.objectToJson(params));
		String lang = ConstLang.getLang(params);
		// 判断入参
		if (null == params ||  params.get("uid") == null || params.get("id") == null || params.get("id").toString().equals("0") || params.get("adTime") == null) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		
		Integer uid = Integer.valueOf(params.get("uid").toString().trim());
		String ip = IpUtil.getIpAddr(request);
		if (redisService.isLuckyExchangeOverRequestLimit(uid,ip)) {
			log.info("luckycreditExchange,uid={},over request limit",uid);
			redisService.recordLuckyExchangeTime(uid);
			return new Result(ResultEnum.REQUEST_FREQUENCY_LIMIT,lang);
		}
		redisService.recordLuckyExchangeTime(uid);
		
		
		Integer id = Integer.valueOf(params.get("id").toString());
		Integer ad_time = Integer.valueOf(params.get("adTime").toString());
		Result rs = luckyCreditService.luckyCreditExchange(id, ad_time,ip);
		rs.changeLange(lang);
		return rs;
		//Integer task_id = Integer.valueOf(params.get("id").toString());
		//return taskIntegralService.playLuckyCredit(user_id, task_id);
	}

}
